# frozen_string_literal: true

module SwitchMirror
  class Node
    VENDORS = {
      'npm' => 'https://registry.npmjs.org',
      'yarn' => 'https://registry.yarnpkg.com',
      'taobao' => 'https://registry.npmmirror.com'
    }.freeze

    def set(mirror_name)
      mirror_url = VENDORS[mirror_name]
      unless mirror_url
        warn "未知的镜像名称：#{mirror_name}"
        exit 2
      end

      system "npm config set registry #{mirror_url}"
      system "yarn config set registry #{mirror_url}"
    end

    def get
      puts "NPM: #{`npm config get registry`}"
      puts "Yarn: #{`yarn config get registry`}"
    end

    def list
      puts <<~END_LIST
        切换 Node 的 npm **和** yarn 的源，可用的提供商包括：

        #{
          VENDORS.keys.map do |vendor|
            "        #{vendor}"
          end.join("\n")
        }

      END_LIST
    end
  end
end
